package com.example.boot2mybatis.service.impl;

import com.example.boot2mybatis.entity.Person;
import com.example.boot2mybatis.entity.User;
import com.example.boot2mybatis.mapper.PersonMapper;
import com.example.boot2mybatis.mapper.UserMapper;
import com.example.boot2mybatis.service.TestTranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TestTranServiceImpl implements TestTranService {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    TransactionManager transactionManager;
    @Override
    public void testTran() {

        testPerson();

        testUser();
    }

    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public  void testPerson() {


        System.out.println("使用的事务管理器是:==>"+transactionManager);
            System.out.println("插入person...");
            Person  person = new Person();
            person.setName("telangpu3");
            personMapper.insert(person);
            System.out.println("制造异常结束！");

    }

    @Transactional(propagation= Propagation.SUPPORTS)
    public  void testUser() {
        System.out.println("插入user...");
        User user= new User();
        user.setPassword("6666");
        user.setName("zhuggg11");
        int i = 1/0;
        userMapper.insert(user);
    }



}
